import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import vuetify from "./plugins/vuetify";
import animated from "animate.css";
import "./assets/css/index.css";
import "./assets/css/iconfont.css";
import "./assets/css/markdown.css";
import "./assets/css/vue-social-share/client.css";
import config from "./assets/js/config";
import Share from "vue-social-share";
import dayjs from "dayjs";
import { vueBaberrage } from "vue-baberrage";
import axios from "axios";
import VueAxios from "vue-axios";
import InfiniteLoading from "vue-infinite-loading";
import "highlight.js/styles/atom-one-dark.css";
import VueImageSwipe from "vue-image-swipe";
import "vue-image-swipe/dist/vue-image-swipe.css";
import Toast from "./components/toast/index";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.prototype.config = config;
Vue.config.productionTip = false;
Vue.use(animated);
Vue.use(Share);
Vue.use(vueBaberrage);
Vue.use(InfiniteLoading);
Vue.use(VueAxios, axios);
Vue.use(VueImageSwipe);
Vue.use(Toast);

Vue.filter("date", function(value) {
  return dayjs(value).format("YYYY-MM-DD");
});

Vue.filter("year", function(value) {
  return dayjs(value).format("YYYY");
});

Vue.filter("hour", function(value) {
  return dayjs(value).format("HH:mm:ss");
});

Vue.filter("time", function(value) {
  return dayjs(value).format("YYYY-MM-DD HH:mm:ss");
});

Vue.filter("num", function(value) {
  if (value >= 1000) {
    return (value / 1000).toFixed(1) + "k";
  }
  return value;
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

router.afterEach(() => {
  window.scrollTo({
    top: 0,
    behavior: "instant"
  });
  NProgress.done();
});

const defaultUrls = [
  "/api/comments/*/like",
  "/api/articles/*/like",
  "/api/talks/*/like",
  "/api/comments",
  "/api/users/info",
  "/api/users/email",
  "/api/users/avatar"
];

// 请求拦截器
axios.interceptors.request.use(config => {
  if (defaultUrls.includes(config.url) || config.url.slice(-4) == "like") {
    // 在发送请求之前做些什么
    let token = localStorage.getItem("token");
    // 判断是否存在token,如果存在将每个页面header添加token
    if (token) {
      config.headers.Authorization = token;
    }
  }
  return config;
});

// 响应拦截器
axios.interceptors.response.use(
  function(response) {
    switch (response.data.code) {
      case 50000: {
        Vue.prototype.$toast({ type: "error", message: "系统异常" });
        break;
      }

      //token 不存在
      case 4000: {
        Vue.prototype.$toast({
          type: "error",
          message: "令牌不存在,请重新登录"
        });
        localStorage.setItem("token", "");
        break;
      }

      //token 过期
      case 4001: {
        Vue.prototype.$toast({
          type: "error",
          message: "令牌已过期,请重新登录"
        });
        localStorage.setItem("token", "");
        break;
      }

      //token 验证不通过
      case 4002: {
        Vue.prototype.$toast({
          type: "error",
          message: "令牌错误,请重新登录"
        });
        localStorage.setItem("token", "");
        break;
      }
    }
    return response;
  },
  function(error) {
    return Promise.reject(error);
  }
);

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount("#app");
